package com.example.addressbook.views;

import java.util.List;

import org.eclipse.jface.layout.RowLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;

import com.example.addressbook.entities.Address;
import com.example.addressbook.services.AddressBookServices;
import com.example.addressbook.services.IAddressService;

public class AddressList extends ViewPart {

	private Label labelLoadAddresses;

	public AddressList() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {

		Composite composite = new Composite(parent, SWT.NONE);
		RowLayoutFactory.swtDefaults().applyTo(composite);

		labelLoadAddresses = new Label(composite, SWT.NONE);
		labelLoadAddresses.setLayoutData(new RowData(140, SWT.DEFAULT));
		labelLoadAddresses.setText("Adressen nicht geladen");

		Button buttonLoadAddresses = new Button(composite, SWT.PUSH);
		buttonLoadAddresses.setText("Adressen laden");
		buttonLoadAddresses.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				IAddressService service = AddressBookServices
						.getAddressService();
				List<Address> allAddresses = service.getAllAddresses();
				labelLoadAddresses.setText(allAddresses.size()
						+ " Adressen geladen");
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	public void setFocus() {
		labelLoadAddresses.setFocus();
	}

}
